package com.code;

import java.sql.ResultSet;
import java.util.ArrayList;

import com.Global.Global;
import com.example.R;
import com.object.obDetailDoanhSoByNV;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class DetailDoanhSoNV extends Activity {
	private String maNV = "";
	ArrayList<Integer> date_actual;
	private String date = "", strNhom = "", strKho = "";
	private LinearLayout llMain;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detaildoanhsonv);
		llMain = (LinearLayout) findViewById(R.id.llMainDetailDSNV);

		maNV = super.getIntent().getExtras().getString("manhanvien");
		date_actual = super.getIntent().getExtras()
				.getIntegerArrayList("date_actual_ds");
		date = String.valueOf(date_actual.get(2))
				+ DetailCuocGoi.setMonth(date_actual.get(1))
				+ Global.formatDay(date_actual.get(0));
		strKho = super.getIntent().getExtras().getString("strKho");
		strNhom = super.getIntent().getExtras().getString("strNhom");		
		Integer choose = super.getIntent().getExtras().getInt("choose_dsnv");

		StringBuffer sql = new StringBuffer(
				" SELECT distinct case when (px.Tennguoimua = '0') then px.Diachi else px.Tennguoimua end as TenKH,vtx.SL as SL,CASE WHEN ISNULL(((vtx.SL * vtx.dongia) ");
		sql.append(" *  (1 + ISNULL(vtx.Thue, 0))),'')='' then 0 else ((vtx.SL * vtx.dongia) ");
		sql.append(" *  (1 + ISNULL(vtx.Thue, 0))) end as ThanhTien");
		sql.append(" FROM Phieuxuat px, vattuxuat vtx ,Products pr ");
		sql.append(" WHERE px.SoCt = vtx.SoCT ");
		sql.append(" AND vtx.SL <> 0 ");
		sql.append(" AND px.DDBH = '" + maNV + "'");
//		if(choose == TK_ChonMau.CHOOSE_NAM)
//			sql.append(" AND DATEPART(YYYY,px.Ngay) = DATEPART(YYYY,'"
//					+ date + "')");
//		if(choose == TK_ChonMau.CHOOSE_QUI)
//			sql.append(" AND DATEPART(quarter,px.Ngay) = DATEPART(quarter,'"
//					+ date + "')");
//		if(choose == TK_ChonMau.CHOOSE_THANG)
//			sql.append(" AND DATEPART(MM,px.Ngay) = DATEPART(MM,'"
//					+ date + "')");
//		if(choose == TK_ChonMau.CHOOSE_TUAN)
//			sql.append(" AND DATEPART(WEEK,px.Ngay) = DATEPART(WEEK,'"
//					+ date + "')");
//		if(choose == TK_ChonMau.CHOOSE_NGAY)
		sql.append(" AND px.Ngay <= '" + date + "' AND px.Ngay >= '" + date
				+ "'");
		if (!strKho.equals("")) {
			sql.append(" AND px.MSK COLLATE DATABASE_DEFAULT ");
			sql.append(" IN (SELECT value FROM  dbo.fn_Split('" + strKho
					+ "', ','))");
		}
		if (!strNhom.equals(""))
		{
			sql.append(" AND vtx.[Product ID] = pr.[Product ID] ");
			sql.append(" AND pr.[Category ID] ='"+strNhom+"'");
		}
		loadData(sql.toString());
	}

	public void loadData(String sql) {
		Float total = 0.0f;
		Integer totalSL = 0;
		ResultSet rs = Global.excuteQuerySQLServer(sql);

		final TableLayout tL = new TableLayout(this);
		llMain.addView(tL);
		TableRow tR = new TableRow(this);
		tR.setWeightSum(4.0f);
		// column nhan vien
		TextView tvNhanVien = new TextView(this);
		tvNhanVien.setText("Khách hàng");
		TK_TongMatHang.setLayoutForTextView(tvNhanVien, true, Gravity.CENTER,
				10, 2.0f);
		// column so luong
		TextView tvSL = new TextView(this);
		tvSL.setText("Số lượng");
		TK_TongMatHang.setLayoutForTextView(tvSL, true, Gravity.CENTER, 10,
				1.0f);
		// column thanh tien
		TextView tvThanhtien = new TextView(this);
		tvThanhtien.setText("Thành tiền");
		TK_TongMatHang.setLayoutForTextView(tvThanhtien, true, Gravity.RIGHT,
				10, 1.0f,true);
		tR.addView(tvNhanVien);
		tR.addView(tvSL);
		tR.addView(tvThanhtien);
		tL.addView(tR);
		try {
			while (rs.next()) {
				final obDetailDoanhSoByNV obDetailDsNV = new obDetailDoanhSoByNV();
				if (rs.getString("TenKH") != null) {
					obDetailDsNV.setTenKH(rs.getString("TenKH"));
				}
				if (rs.getString("SL") != null) {
					obDetailDsNV.setSL(String.valueOf(rs.getInt("SL")));
					totalSL = totalSL + rs.getInt("SL");
				}
				if (rs.getString("ThanhTien") != null) {
					obDetailDsNV.setThanhTien(rs.getString("ThanhTien"));
					total = total + Float.valueOf(rs.getString("ThanhTien"));
				}

				TableRow tRow = new TableRow(this);
				tRow.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						for (int i = 0; i < tL.getChildCount(); i++) {
							tL.getChildAt(i).setBackgroundColor(
									Color.TRANSPARENT);
						}
						v.setBackgroundColor(Color.YELLOW);
					}
				});

				tRow.setWeightSum(4.0f);
				// column Khach hang
				TextView tvNhanViens = new TextView(this);
				tvNhanViens.setText(obDetailDsNV.getTenKH());
				TK_TongMatHang.setLayoutForTextView(tvNhanViens, false,
						Gravity.CENTER, 10, 2.0f);
				// column so luong
				TextView tvSLs = new TextView(this);
				tvSLs.setText(obDetailDsNV.getSL());
				TK_TongMatHang.setLayoutForTextView(tvSLs, false,
						Gravity.CENTER, 10, 1.0f);
				// column thanh tien
				TextView tvThanhtiens = new TextView(this);
				tvThanhtiens.setText(Global.formatDecimal(Float
						.valueOf(obDetailDsNV.getThanhTien())));
				TK_TongMatHang.setLayoutForTextView(tvThanhtiens, false,
						Gravity.RIGHT, 10, 1.0f,true);
				tRow.addView(tvNhanViens);
				tRow.addView(tvSLs);
				tRow.addView(tvThanhtiens);
				tL.addView(tRow);
			}

			// Rows End
			TableRow tREnd = new TableRow(this);
			tREnd.setWeightSum(4.0f);

			TextView tvEndNV = new TextView(this);
			tvEndNV.setText("Tổng");
			TK_TongMatHang.setLayoutForTextView(tvEndNV, false, Gravity.CENTER,
					10, 2.0f);
			tvEndNV.setBackgroundResource(R.layout.border_topleftbottom);

			TextView tvEndSL = new TextView(this);
			tvEndSL.setText(String.valueOf(totalSL));
			TK_TongMatHang.setLayoutForTextView(tvEndSL, false, Gravity.CENTER,
					10, 1.0f);
			tvEndSL.setBackgroundResource(R.layout.border_topleftbottom);

			TextView tvEndThanhTien = new TextView(this);
			tvEndThanhTien.setText(Global.formatDecimal(total).toString());
			TK_TongMatHang.setLayoutForTextView(tvEndThanhTien, false,
					Gravity.RIGHT, 10, 1.0f,true);
			tvEndThanhTien.setBackgroundResource(R.layout.border);

			tREnd.addView(tvEndNV);
			tREnd.addView(tvEndSL);
			tREnd.addView(tvEndThanhTien);
			tL.addView(tREnd);
			rs.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}
